import 'package:bot_toast/bot_toast.dart';
import 'package:get/get.dart';

import '../../services/address.dart';
import '../../services/dio_manager.dart';
import '../home_module/home_model.dart';
import 'all_secruty_model.dart';

class AllSecrutyController extends GetxController {
  @override
  void onInit() {
    // TODO: implement onInit
    super.onInit();
    requestData();
  }

  AppGridModel? model;

  var dataArr = [];

  /// 列表拖动
  listMove(int oldIndex, int newIndex, int index) {
    print("$oldIndex to $newIndex");
    if (oldIndex < newIndex) {
      //向下移动的情况,需要减去1，不然会index 溢出
      newIndex -= 1;
    }
    // final int item = itemList.removeAt(oldIndex);
    // itemList.insert(newIndex, item);

    Apps reapp = model!.data![index].apps!.removeAt(oldIndex);
    model!.data![index].apps!.insert(newIndex, reapp);
    update();
  }

  /// 获取我的全部
  requestData() async {
    var params = {
      'page': '1',
      'limit': '100',
    };

    var json = await DioManager()
        .kkRequest(Address.appGrid, params: params, method: 'get');

    AppGridModel _model = AppGridModel.fromJson(json);

    model = _model;

    dataArr.add(_model);

    update();
  }

  /// 关注（添加）应用
  requestDataWithFocus(var id) async {

    var params = {
      'grid_id':id
    };
    var json = await DioManager().kkRequest(Address.appGridFocus,bodyParams: params);
    BotToast.showText(text: json['message']);
    update();
  }


  /// 移除关注应用
  requestDataWithRemove(var id) async {

    var params = {
      'grid_id':id
    };
    var json = await DioManager().kkRequest(Address.appGridRemove,bodyParams: params);

    BotToast.showText(text: json['message']);
    update();
  }


}
